#include <catch2/catch.hpp>

#include "037_hard.hpp"

using namespace jianzhi_037;

TEST_CASE("037", "[JIANZHI][037]")
{

Codec codec;

SECTION("deserialize") {
    SECTION("example 1") {
        auto root = codec.deserialize("[1,2,3,null,null,4,5]");

        CHECK(root->val == 1);
        CHECK(root->left->val == 2);
        CHECK(root->right->val == 3);

        CHECK(root->left->left == nullptr);
        CHECK(root->left->right == nullptr);

        CHECK(root->right->left->val == 4);
        CHECK(root->right->right->val == 5);

        CHECK(root->right->left->left == nullptr);
        CHECK(root->right->left->right == nullptr);

        CHECK(root->right->right->left == nullptr);
        CHECK(root->right->right->right == nullptr);

        delete root;
    }

    SECTION("example 2") {
        auto root = codec.deserialize("[-1,0,1]");

        CHECK(root->val == -1);
        CHECK(root->left->val == 0);
        CHECK(root->right->val == 1);

        CHECK(root->left->left == nullptr);
        CHECK(root->left->right == nullptr);

        CHECK(root->right->left == nullptr);
        CHECK(root->right->right == nullptr);

        delete root;
    }

    SECTION("example 3") {
        auto root = codec.deserialize("[1]");

        CHECK(root->val == 1);
        CHECK(root->left == nullptr);
        CHECK(root->left == nullptr);

        delete root;
    }

    SECTION("example 4") {
        auto root = codec.deserialize("[]");
        CHECK(root == nullptr);
    }

    SECTION("example 5") {
        auto root = codec.deserialize("[1,3,null,null,4]");
        CHECK(root->val == 1);
        CHECK(root->left->val == 3);
        CHECK(root->right == nullptr);

        CHECK(root->left->left == nullptr);
        CHECK(root->left->right->val == 4);
    }
}

SECTION("serialize") {
    SECTION("example 1") {
        TreeNode root(1, new TreeNode(2), new TreeNode(3, new TreeNode(4), new TreeNode(5)));
        CHECK(codec.serialize(&root) == "[1,2,3,null,null,4,5]");
    }

    SECTION("example 2") {
        TreeNode root(-1, new TreeNode(0), new TreeNode(1));
        CHECK(codec.serialize(&root) == "[-1,0,1]");
    }

    SECTION("example 3") {
        TreeNode root(1);
        CHECK(codec.serialize(&root) == "[1]");
    }

    SECTION("example 4") {
        CHECK(codec.serialize(nullptr) == "[]");
    }

    SECTION("example 5") {
        TreeNode root(1, new TreeNode(3, nullptr, new TreeNode(4)), nullptr);
        CHECK(codec.serialize(&root) == "[1,3,null,null,4]");
    }
}

}
